package com.example.networklocationtracker;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class LocationResult extends Activity {
TextView tv;
int cnt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_result);
        tv=(TextView)findViewById(R.id.tv1);
        cnt=1;
        //Updated
     // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(getApplicationContext().LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
             // makeUseOfNewLocation(location);
            	Toast.makeText(getApplicationContext(), "Test "+location, 5000).show();
            	//tv.setText("::"+location);
            	//Log.i("Location Man:: "+location,"test" );
                Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
                String[] lat,lan;
				Double lt,ln;
                  lt=location.getLatitude();
//                lat= location.toString().split("mLatitude=");
//                lt=Double.parseDouble(lat[0].toString());
//                Log.i("Lat", ""+lt);
                
                  ln=location.getLongitude();
//                lan= location.toString().split("mLongitude=");
//                ln=Double.parseDouble(lan[0].toString());
//                Log.i("Lan", ""+ln);
                
                List<Address> addresses;
         	try {
             	//Toast.makeText(getApplicationContext(), "Try ", 5000).show(); 
         		addresses = gcd.getFromLocation(lt, ln, 1);
         		if (addresses.size() > 0) 
         		tv.setText("Location Name is countert "+location+"::"+ cnt + "   :: "+addresses.get(0).getLocality());
         		
         		} catch (IOException e) {
         		// TODO Auto-generated catch block
             	//Toast.makeText(getApplicationContext(), "Err "+e, 5000).show();
         			tv.setText("Location Not found"+cnt);
         			
         		e.printStackTrace();
         		
         	}
         	cnt=cnt+1;
            	
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
          };
          
       // LocationManager locationManager = (LocationManager) this.getSystemService(getApplicationContext().LOCATION_SERVICE);
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000,  locationListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_location_result, menu);
        return true;
    }
    
}
